/*
 * @(#)CookieUtil.java		1.0.0	2012-05-15
 * 
 * Copyright (c) 2012 Aspire Info (Beijing), Inc. All Right Reserved.
 */


package com.aspire.wap.core.http;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aspire.pams.tag.sichuan.tools.userInfo.UserInfo;

/**
 * 
 * @author		Mao Xuguang
 * @version		1.0.0	2012-05-15
 */
public class CmnetCookieUtil {
	
	public static class User {
		
		public String mobile;
		public String loginId;
		
		public User(String mobile, String loginId) {
			this.mobile = mobile;
			this.loginId = loginId;
		}
	}

	public static void set(HttpServletResponse response, User user) {
		
		try {
			String value = format(user.mobile, user.loginId);
			setCookie(response, CMNetConstants.CODEANDCLOSE, value, 300);
		} catch (Exception e) {
		}
	}
	
	public static User get(HttpServletRequest request) {
		
		try {
			String value = getCookieValue(request, CMNetConstants.CODEANDCLOSE);
			String[] s = parse(value);
			return new User(s[0], s[1]);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String getMobileFromCookie(HttpServletRequest request) {
		
		User user = CmnetCookieUtil.get(request);
		return user == null ? null : user.mobile;
	}
	
	private static String getCookieValue(HttpServletRequest request, String name) {
		
		Cookie[] cks = request.getCookies();
		for (int i = 0; cks != null && i < cks.length; i++) {
			if (name.equals(cks[i].getName())) {
				return cks[i].getValue();
			}
		}
		return null;
	}
	
	private static void setCookie(HttpServletResponse response, String name, String value, int time) {
		
		Cookie ck = new Cookie(name, value);
		ck.setMaxAge(time);
		ck.setPath("/");
		response.addCookie(ck);
	}
	
	private static String format(String...str) throws IOException {
		
		StringBuilder buff = new StringBuilder();
		for (int i = 0; str != null && i < str.length; i++) {
			buff.append(encode(str[i]));
			if (i < str.length - 1) {
				buff.append('|');
			}
		}
		return buff.toString();
	}
	
	private static String[] parse(String str) throws IOException {

		String[] s = str.split("\\|");
		for (int i = 0; i < s.length; i++) {
			s[i] = decode(s[i]);
		}
		return s;
	}
	
	private static String encode(String str) throws IOException {
		
		str = compress(str);
		str = URLEncoder.encode(str, "UTF8");
		return str;
	}
	
	private static String decode(String str) throws IOException {
		
		str = URLDecoder.decode(str, "UTF8");
		str = uncompress(str);
		return str;
	}
	
	private static String compress(String str) throws IOException {
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(out);
		gzip.write(str.getBytes());
		gzip.close();
		return out.toString("ISO-8859-1");
	}
	
	private static String uncompress(String str) throws IOException {
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[256];
		
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		return out.toString();
	}
	
	
	/**
	 * 2012-09-27 舒伟提供
	 * @param request
	 * @return
	 */
	public static String getSCMobile( HttpServletRequest request )
	{
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(CMNetConstants.CODEANDCLOSE);
	    if(userInfo == null){
	       return null;
	    }
	    return userInfo.getMobile();
	}

	
	public static final String CODEANDCLOSE = "sc_cmnet_code";
}
